package edu.bsu.android.gtv.apps.bconnected.util;

import java.util.ArrayList;

import com.google.android.imageloader.ImageLoader;

import android.app.Activity;
import android.view.LayoutInflater; 
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import edu.bsu.android.gtv.apps.bconnected.objects.SISession;
import edu.bsu.android.gtv.apps.bconnected.objects.Tutor;
import edu.bsu.android.gtv.apps.bconnected.util.UIUtils;
import edu.bsu.android.gtv.apps.bconnected.R;
import edu.bsu.android.gtv.apps.bconnected.objects.Category;
import edu.bsu.android.gtv.apps.bconnected.objects.Content;
import edu.bsu.android.gtv.apps.bconnected.objects.Course;
import edu.bsu.android.gtv.apps.bconnected.objects.Degree;
import edu.bsu.android.gtv.apps.bconnected.objects.Event;
import edu.bsu.android.gtv.apps.bconnected.objects.EventType;
import edu.bsu.android.gtv.apps.bconnected.objects.Month;
import edu.bsu.android.gtv.apps.bconnected.objects.Organization;
import edu.bsu.android.gtv.apps.bconnected.objects.Person;
import edu.bsu.android.gtv.apps.bconnected.objects.Term;

public class Adapters {
    /**
     * CategoryAdapter used to populate the listview for the categories
     */
    public static class CategoryAdapter extends ArrayAdapter<Category> {
        private ArrayList<Category> items;
        private Activity activity;

        public CategoryAdapter(Activity activity, int textViewResourceId, ArrayList<Category> items) {
            super(activity.getApplicationContext(), textViewResourceId, items);
            this.items = items; 
            this.activity = activity;
        }
        
        @Override
        public View getView(int position, View convertView, ViewGroup parent)  {           	
            View view = convertView;
            
            if (view == null) {
            	view = LayoutInflater.from(activity).inflate(R.layout.list_item, null);
            } 
            
            Category category = items.get(position);    
            
            if (category != null) {
            	UIUtils.displayTextView(view, R.id.item, category.getCategoryName());
            }
            
            return view;
        } 
    }
    
    /**
     * ContentAdapter used to populate the listview for the web posts
     */
    public static class ContentAdapter extends ArrayAdapter<Content> {
        private ArrayList<Content> items;
        private Activity activity;

        public ContentAdapter(Activity activity, int textViewResourceId, ArrayList<Content> items) {
            super(activity.getApplicationContext(), textViewResourceId, items);
            this.items = items;
            this.activity = activity;
        }
        
        @Override
        public View getView(int position, View convertView, ViewGroup parent)  { 
            final View view = LayoutInflater.from(activity).inflate(R.layout.list_item_content, null);
            ImageLoader imageLoader = new ImageLoader();
            
            final Content content = items.get(position);    
            
            if (content != null) {            	  
            	UIUtils.displayTextView(view, R.id.category, content.getCategoryName()); 
            	UIUtils.displayTextView(view, R.id.headline, content.getHeadline());
            	UIUtils.displayTextView(view, R.id.tagline, content.getTagline());
            	
            	if (content.getThumbURL() != null) {
	                ImageView image = (ImageView) view.findViewById(R.id.photo);
	                image.setTag(content.getThumbURL().toString());
	                image.setContentDescription(content.getHeadline());
	                imageLoader.bind(image, content.getThumbURL().toString(), null);
            	}
            }
            
            return view; 
        } 
    }
    
    /**
     * CourseAdapter used to populate the listview for the courses
     */
    public static class CourseAdapter extends ArrayAdapter<Course> {
        private ArrayList<Course> items;
        private Activity activity;
        private int target;
        private static final int TYPE_ITEM = 0;
        private static final int TYPE_SEPARATOR = 1;

        public CourseAdapter(Activity activity, int textViewResourceId, ArrayList<Course> items, int target) {
            super(activity.getApplicationContext(), textViewResourceId, items);
            this.items = items;
            this.activity = activity;
            this.target = target;
        }
        
        public CourseAdapter(Activity activity, ArrayList<Course> items, int target) {
            super(activity.getApplicationContext(), 0, items);
            this.items = items;
            this.activity = activity;
            this.target = target;
        }
        
        @Override
		public boolean areAllItemsEnabled() {
            return false;
        }

        @Override
        public boolean isEnabled(int position) {
        	if (getItemViewType(position) == TYPE_SEPARATOR) {  
                return false;
            }
            return super.isEnabled(position);
        }
 
        @Override
        public int getItemViewType(int position) {
        	if (items.get(position).getCourseWithTitle().equals("_Header") || items.get(position).getCourseWithTitle().equals("_Footer")) {
        		return TYPE_SEPARATOR;
        	} else {
        		return TYPE_ITEM;
        	}
        }
        
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {                	
            View view = convertView;
            
            if (view == null) {
            	switch (target) {
	            	case Constants.TARGET_SCHEDULE:
	                	view = LayoutInflater.from(activity).inflate(R.layout.list_item_secondary, null);
	            		break;
	            	case Constants.TARGET_OTHER_COURSE:
	            	case Constants.TARGET_SHARED_COURSE:
	                	view = LayoutInflater.from(activity).inflate(R.layout.list_item_tab_twoline, null); 
	            		break;
            	}
            } 
            
            Course course = items.get(position);    
            
            if (course != null) {
            	switch (target) {
	            	case Constants.TARGET_SCHEDULE:
	                	if (course.getError() != null) {
	                    	view.findViewById(R.id.footer).setVisibility(View.GONE);
	                    	UIUtils.displayTextView(view, R.id.item, course.getError());
	                	} else {        	
		                	if (course.getCourseWithTitle().equals("_Footer")) {
		                    	UIUtils.displayTextView(view, R.id.footer,
		                    			activity.getString(R.string.content_total_credit_hours) + " " + course.getTotalCreditHours());
		                    	view.findViewById(R.id.item).setVisibility(View.GONE);
		                	} else {
		                    	view.findViewById(R.id.footer).setVisibility(View.GONE);
		                		UIUtils.displayTextView(view, R.id.item, course.getCourseWithTitle());
		                	}
	                	} 
	            		break;
	            	case Constants.TARGET_OTHER_COURSE:
	                	UIUtils.displayTextView(view, R.id.title, course.getCourseWithTitle() + " - Section " + course.getSection()); 
	                	UIUtils.displayTextView(view, R.id.desc, course.getTermFullDesc());
	            		break;
	            	case Constants.TARGET_SHARED_COURSE:
	                	UIUtils.displayTextView(view, R.id.title, course.getCourseWithTitle());
	                	UIUtils.displayTextView(view, R.id.desc, course.getTermFullDesc());
	            		break;
            	}
            }
            
            return view;
        }
    }

    /**
     * DegreeAdapter used to populate the listview for the degrees
     */
    public static class DegreeAdapter extends ArrayAdapter<Degree> {
        private ArrayList<Degree> items;
        private Activity activity;
        
        public DegreeAdapter(Activity activity, ArrayList<Degree> items) {
            super(activity.getApplicationContext(), 0, items);
            this.items = items;
            this.activity = activity;
        }
        
        @Override
        public View getView(int position, View convertView, ViewGroup parent)  {        	
            View view = convertView;
            
            if (view == null) {
            	view = LayoutInflater.from(activity).inflate(R.layout.list_item_tab_degree, null);
            }
            
            Degree degree = items.get(position);  
             
            if (degree != null) {
            	UIUtils.displayTextView(view, R.id.degree, degree.getDegreeType());
            	UIUtils.displayTextView(view, R.id.program, degree.getProgramOption());
            	UIUtils.displayTextView(view, R.id.program_type, degree.getProgramType());
            	UIUtils.displayTextView(view, R.id.department, degree.getDepartment());
            }
             
            return view;
        }
    }

    /**
     * EventAdapter used to populate the listview for the events
     */
    public static class EventAdapter extends ArrayAdapter<Event> {
        private ArrayList<Event> items;
        private Activity activity;
        private int target;

        public EventAdapter(Activity activity, int textViewResourceId, ArrayList<Event> items, int target) {
            super(activity.getApplicationContext(), textViewResourceId, items);
            this.items = items;
            this.activity = activity;
            this.target = target;
        }
        
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {                     	
            View view = convertView;
            
            if (view == null) {
            	view = LayoutInflater.from(activity).inflate(R.layout.list_item_event, null);
            } 
            
            Event event = items.get(position);    
            
            if (event != null) {
            	if (event.getError() != null) {
                	UIUtils.displayTextView(view, R.id.name, event.getError());
            		view.findViewById(R.id.date).setVisibility(View.GONE);
            		view.findViewById(R.id.category).setVisibility(View.GONE);
            	} else {        	
                	String eventDate = event.getBeginDate();

        	    	if (event.getEndDate().length() > 0 && !event.getEndDate().matches(event.getBeginDate())) {
        	    		eventDate = eventDate + " - " + event.getEndDate();
        	    	}  
        	    	
                	UIUtils.displayTextView(view, R.id.date, eventDate); 
                	UIUtils.displayTextView(view, R.id.name, event.getEventName());
                	
                	if (target == Constants.TARGET_EVENT_MONTH) {
                		UIUtils.displayTextView(view, R.id.category, event.getCalendarEventName());
                	} else {
                		view.findViewById(R.id.category).setVisibility(View.GONE);
                	}
            	} 
            }
            
            return view;
        }
    }

    /**
     * EventTypeAdapter used to populate the listview for the months
     */
    public static class EventTypeAdapter extends ArrayAdapter<EventType> {
        private ArrayList<EventType> items;
        private Activity activity;
        
        public EventTypeAdapter(Activity activity, int textViewResourceId, ArrayList<EventType> items) {
            super(activity.getApplicationContext(), textViewResourceId, items);
            this.items = items; 
            this.activity = activity;
        }
        
        @Override
        public View getView(int position, View convertView, ViewGroup parent)  {                  	
            View view = convertView;
            
            if (view == null) {
            	view = LayoutInflater.from(activity).inflate(R.layout.list_item, null);
            }

            EventType eventType = items.get(position);    
            
            if (eventType != null) {
            	UIUtils.displayTextView(view, R.id.item, eventType.getCalendarEventName());
            }
            
            return view;
        } 
    }

    /**
     * MonthAdapter used to populate the listview for the months
     */
    public static class MonthAdapter extends ArrayAdapter<Month> {
        private ArrayList<Month> items;
        private Activity activity;
        
        public MonthAdapter(Activity activity, int textViewResourceId, ArrayList<Month> items) {
            super(activity.getApplicationContext(), textViewResourceId, items);
            this.items = items; 
            this.activity = activity;
        }
        
        @Override
        public View getView(int position, View convertView, ViewGroup parent)  {                  	
            View view = convertView;
            
            if (view == null) {
            	view = LayoutInflater.from(activity).inflate(R.layout.list_item, null);
            }

            Month month = items.get(position);    
            
            if (month != null) {
            	UIUtils.displayTextView(view, R.id.item, month.getMonthWithYear());
            }
            
            return view;
        } 
    }

    /**
     * OrganizationAdapter used to populate the listview for the organizations
     */
    public static class OrganizationAdapter extends ArrayAdapter<Organization> {
        private ArrayList<Organization> items;
        private Activity activity;
        private int target;

        public OrganizationAdapter(Activity activity, int textViewResourceId, ArrayList<Organization> items, int target) {
            super(activity.getApplicationContext(), textViewResourceId, items);
            this.items = items;
            this.activity = activity;
            this.target = target;
        }
        
        public OrganizationAdapter(Activity activity, ArrayList<Organization> items, int target) {
            super(activity.getApplicationContext(), 0, items);
            this.items = items;
            this.activity = activity;
            this.target = target;
        }
        
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {     	
            View view = convertView;
            
            if (view == null) {
            	switch (target) {
	            	case Constants.TARGET_STUDENT_ORG:
	                	view = LayoutInflater.from(activity).inflate(R.layout.list_item_secondary, null);
	            		break;
	            	case Constants.TARGET_SHARED_ORGANIZATION:
	                	view = LayoutInflater.from(activity).inflate(R.layout.list_item_tab_twoline, null);
	            		break;
            	}
            }
            
            Organization org = items.get(position);    
            
            if (org != null) {
            	switch (target) {
	            	case Constants.TARGET_STUDENT_ORG:
	                	if (org.getError() != null) {
	                    	UIUtils.displayTextView(view, R.id.item, org.getError());
	                	} else {
	                    	UIUtils.displayTextView(view, R.id.item, org.getOrganizationName());
	                	}         	
	            		break;
	            	case Constants.TARGET_SHARED_ORGANIZATION:
	                	UIUtils.displayTextView(view, R.id.title, org.getOrganizationName());
	                	UIUtils.displayTextView(view, R.id.desc, org.getCategory());
	            		break;
	        	}
            }
            
            return view;
        }
    }

    /**
     * PersonAdapter used to populate the listview for the various "person" objects
     * {@link Advisor}
     * {@link Classmate}
     * {@link Connection}
     * {@link Instructor}
     * {@link Member}
     * {@link Roommate}
     */
    public static class PersonAdapter extends ArrayAdapter<Person> {
        private ArrayList<Person> items;
        private Activity activity;
        private int target;

        public PersonAdapter(Activity activity, int textViewResourceId, ArrayList<Person> items, int target) {
            super(activity.getApplicationContext(), textViewResourceId, items);
            this.items = items;
            this.activity = activity;
            this.target = target;
        }
        
        public PersonAdapter(Activity activity, ArrayList<Person> items, int target) {
            super(activity.getApplicationContext(), 0, items);
            this.items = items;
            this.activity = activity;
            this.target = target;
        }
        
        @Override
        public View getView(int position, View convertView, ViewGroup parent)  {        	
            View view = convertView;
            
            if (view == null) { 
            	switch (target) {
	            	case Constants.TARGET_ADVISOR:
	                	view = LayoutInflater.from(activity).inflate(R.layout.list_item_tab_advisor, null);
	            		break;
	            	case Constants.TARGET_CLASSMATE:
	                	view = LayoutInflater.from(activity).inflate(R.layout.list_item_tab_classmate, null);
	            		break;
	            	case Constants.TARGET_CONNECTION:
	                	view = LayoutInflater.from(activity).inflate(R.layout.list_item, null);
	                	break;
	            	case Constants.TARGET_INSTRUCTOR:
	                	view = LayoutInflater.from(activity).inflate(R.layout.list_item_tab_instructor, null);
	                	break;
	            	case Constants.TARGET_MEMBER:
	            	case Constants.TARGET_ROOMMATE:
	                	view = LayoutInflater.from(activity).inflate(R.layout.list_item_tab_person, null);
	                	break;    
            	}
            }
            
            Person person = items.get(position);  
             
            if (person != null) {
            	String name = (person.getLastName() + " " + person.getNameSuffix()).trim() + ", " + person.getFirstName();
            	
            	switch (target) {
	            	case Constants.TARGET_ADVISOR:
	                	UIUtils.displayTextView(view, R.id.name, person.getAdvisorName());
	                	UIUtils.displayTextView(view, R.id.type, person.getAdvisorType());
	                	UIUtils.displayTextView(view, R.id.advising_center, person.getAdvisingCenter());
	                	UIUtils.displayTextView(view, R.id.office, person.getOffice());
	                	UIUtils.displayTextView(view, R.id.phone, person.getPhone());
	            		break;
	            	case Constants.TARGET_CLASSMATE:
	                	UIUtils.displayTextView(view, R.id.item, name + " (" + person.getCourseCount() + ")");
	                	
	                	if (person.getStarred()) {
	                    	((ImageView) view.findViewById(R.id.star_button)).setVisibility(View.VISIBLE);
	                	} else {
	                    	((ImageView) view.findViewById(R.id.star_button)).setVisibility(View.GONE);
	                	}
	                	break;
	            	case Constants.TARGET_CONNECTION:
	                	if (person.getError() != null) {
	                    	UIUtils.displayTextView(view, R.id.item, person.getError());
	                	} else {
	                    	UIUtils.displayTextView(view, R.id.item, name);  
	                	}         	
	            		break;
	            	case Constants.TARGET_INSTRUCTOR:
	                	UIUtils.displayTextView(view, R.id.name, (person.getNamePrefix() + " " + person.getFirstName() + " " + 
	                			person.getLastName() + " " + person.getNameSuffix()).trim());
	                	UIUtils.displayTextView(view, R.id.title, person.getTitleRank());
	                	UIUtils.displayTextView(view, R.id.department, person.getDepartment());
	                	
	                	if (person.getStarred()) {
	                    	((ImageView) view.findViewById(R.id.star_button)).setVisibility(View.VISIBLE);
	                	} else {
	                    	((ImageView) view.findViewById(R.id.star_button)).setVisibility(View.GONE);
	                	}
	            		break;
	            	case Constants.TARGET_MEMBER:
	                	UIUtils.displayTextView(view, R.id.title, name);
	                	
	                	if (Integer.parseInt(person.getMemberTypeID()) != Constants.VALUE_MEMBER_TYPE_GENERAL) {
	                    	UIUtils.displayTextView(view, R.id.desc, person.getMemberRole() );
	                	} else {
	                    	((TextView) view.findViewById(R.id.desc)).setVisibility(View.GONE);
	                	}
	                	
	                	if (person.getStarred()) {
	                    	((ImageView) view.findViewById(R.id.star_button)).setVisibility(View.VISIBLE);
	                	} else {
	                    	((ImageView) view.findViewById(R.id.star_button)).setVisibility(View.GONE);
	                	}
	                	break;
	            	case Constants.TARGET_ROOMMATE:
	                	UIUtils.displayTextView(view, R.id.title, name);
	                	
	                	if (Integer.parseInt(person.getPersonType()) == Constants.VALUE_PERSON_TYPE_ROOMMATE ||
	                			Integer.parseInt(person.getPersonType()) == Constants.VALUE_PERSON_TYPE_RA ||
	                			Integer.parseInt(person.getPersonType()) == Constants.VALUE_PERSON_TYPE_DIRECTOR) { 
	                    	UIUtils.displayTextView(view, R.id.desc, person.getTitleRank());
	                	} else {
	                    	((TextView) view.findViewById(R.id.desc)).setVisibility(View.GONE);
	                	}
	                	
	                	if (person.getStarred()) {
	                    	((ImageView) view.findViewById(R.id.star_button)).setVisibility(View.VISIBLE);
	                	} else {
	                    	((ImageView) view.findViewById(R.id.star_button)).setVisibility(View.GONE);
	                	}
	            		break;
            	}
            }
             
            return view;
        }
    }

    /**
     * SISessionAdapter used to populate the listview for the supplemental instruction sessions
     */
    public static class SISessionAdapter extends ArrayAdapter<SISession> {
        private ArrayList<SISession> items;
        private Activity activity;
        
        public SISessionAdapter(Activity activity, ArrayList<SISession> items) {
            super(activity.getApplicationContext(), 0, items);
            this.items = items;
            this.activity = activity;
        }
        
        @Override
        public View getView(int position, View convertView, ViewGroup parent)  {        	
            View view = convertView;
            
            if (view == null) {
	        	view = LayoutInflater.from(activity).inflate(R.layout.list_item_tab_twoline, null);
            }
            
            SISession session = items.get(position);  
             
            if (session != null) {
            	UIUtils.displayTextView(view, R.id.title, session.getBuildingDesc() + ", " + 
            			activity.getString(R.string.content_room) + " " + session.getRoom());
            	UIUtils.displayTextView(view, R.id.desc, session.getDay() + " " + session.getBeginTime() + " - " + session.getEndTime());
            }
             
            return view;
        }
    }

    /**
     * TermAdapter used to populate the listview for the terms
     */
    public static class TermAdapter extends ArrayAdapter<Term> {
        private ArrayList<Term> items;
        private Activity activity;

        public TermAdapter(Activity activity, int textViewResourceId, ArrayList<Term> items) {
            super(activity.getApplicationContext(), textViewResourceId, items);
            this.items = items;
            this.activity = activity;
        }
        
        @Override
        public View getView(int position, View convertView, ViewGroup parent)  {           	
            View view = convertView;
            
            if (view == null) {
            	view = LayoutInflater.from(activity).inflate(R.layout.list_item, null);
            }
            
            Term term = items.get(position);    
            
            if (term != null) {
            	UIUtils.displayTextView(view, R.id.item, term.getTermFullDesc());
            }
            
            return view;
        } 
    }
    /**
     * TutorAdapter used to populate the listview for the tutors
     * This view is more than the standard Android ListView - this uses a layout and displays multiple rows of text.
     */
    public static class TutorAdapter extends ArrayAdapter<Tutor>  {
        private ArrayList<Tutor> items;
        private Activity activity;

        public TutorAdapter(Activity activity, ArrayList<Tutor> items) {
            super(activity.getApplicationContext(), 0, items);
            this.items = items;
            this.activity = activity;
        }
        
        @Override
        public View getView(int position, View convertView, ViewGroup parent)  {                    	
            View view = convertView;
            
            if (view == null) {
            	view = LayoutInflater.from(activity).inflate(R.layout.list_item_tab_instructor, null);
            } 
            
            Tutor tutor = items.get(position); 
            
            if (tutor != null) {
            	UIUtils.displayTextView(view, R.id.name, (tutor.getNamePrefix() + " " + tutor.getFirstName() + 
            			" " + tutor.getLastName() + " " + tutor.getNameSuffix()).trim());
            	UIUtils.displayTextView(view, R.id.title, tutor.getTitleRank());
            	UIUtils.displayTextView(view, R.id.department, tutor.getDepartment());
            	
            	if (tutor.getStarred()) {
                	((ImageView) view.findViewById(R.id.star_button)).setVisibility(View.VISIBLE);
            	} else {
                	((ImageView) view.findViewById(R.id.star_button)).setVisibility(View.GONE);
            	}
            }
            
            return view;
        }
    }
}
